package com.yykj.common.util;

import org.json.JSONObject;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chendg on 2019/7/19 0019.
 */
public class LocalObjectUtil {

	public static void copyNullValue(Object src, Object target) {
		Map<String, Object> map = objectToMap(src);
		for (Field field : target.getClass().getDeclaredFields()) {
			String fieldName = field.getName();
			if ("serialVersionUID".equals(fieldName)
					|| "Object".equals(field.getType().getSimpleName())) {
				continue;
			}
			field.setAccessible(true);
			try {
				if (!ObjectUtils.isEmpty(map.get(fieldName))) {
					if(field.getGenericType().getTypeName().equals(map.get(fieldName).getClass().getTypeName())){
						field.set(target, map.get(fieldName));
					}
				}

			}
			catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	public static Map<String, Object> objectToMap(Object obj) {
		Map<String, Object> map = new HashMap<>();
		for (Field field : obj.getClass().getDeclaredFields()) {
			String fieldName = field.getName();
			if ("serialVersionUID".equals(fieldName)
					|| "Object".equals(field.getType().getSimpleName())) {
				continue;
			}
			field.setAccessible(true);
			try {
				Object object = field.get(obj);
				map.put(fieldName, object);
			}
			catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	public static Map<String, String> objectToStringStringMap(Object obj,
			boolean needNullField) {
		Map<String, String> map = new HashMap<>();
		for (Field field : obj.getClass().getDeclaredFields()) {
			String fieldName = field.getName();
			if ("serialVersionUID".equals(fieldName)
					|| "Object".equals(field.getType().getSimpleName())) {
				continue;
			}
			field.setAccessible(true);
			try {
				Object object = field.get(obj);
				if (object == null) {
					if (needNullField) {
						map.put(fieldName, "");
					}
				}
				else {
					map.put(fieldName, object.toString());
				}
			}
			catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	public static JSONObject objToJSONObject(Object obj) {
		JSONObject jsonObject = new JSONObject();
		Map<String, Object> map = objectToMap(obj);
		for (String key : map.keySet()) {
			jsonObject.put(key, map.get(key));
		}
		return jsonObject;
	}

	public static void setStringFieldEmptyString(Object obj) {
		for (Field field : obj.getClass().getDeclaredFields()) {
			String fieldName = field.getName();
			if ("serialVersionUID".equals(fieldName)
					|| "Object".equals(field.getType().getSimpleName())) {
				continue;
			}
			field.setAccessible(true);
			try {
				if (field.getType().equals(String.class)) {
					if (field.get(obj) == null) {
						field.set(obj, "");
					}
				}
				if (field.getType().equals(Integer.class)) {
					if (field.get(obj) == null) {
						field.set(obj, 0);
					}
				}
			}
			catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

}
